package com.lyw.spring.demo.controller;


import com.lyw.spring.demo.model.MessageInfo;
import com.lyw.spring.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

//    private List<MessageInfo> messageInfoList = new ArrayList<>();

    @RequestMapping(value = "/publish", produces = "application/json")
    public String publish(@RequestBody MessageInfo messageInfo) {
        if (!StringUtils.hasLength(messageInfo.getFrom())
                || !StringUtils.hasLength(messageInfo.getTo())
                || !StringUtils.hasLength(messageInfo.getMessage())) {

            return "{\"ok\": 0}";
        }

//        messageInfoList.add(messageInfo);
        //TODO: 存储到数据库
        messageService.addMessage(messageInfo);

        return "{\"ok\": 1}";
    }

    @RequestMapping("/getList")
    public List<MessageInfo> getList() {
        return messageService.queryAll();
    }
}
